package cn.kgc.contact.service;

import cn.kgc.contact.mapper.ContactMapper;
import cn.kgc.contact.pojo.Contact;
import cn.kgc.contact.pojo.ContactExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ContactServiceImpl implements  ContactService {

    @Resource
    ContactMapper contactMapper;

    @Override
    public PageInfo<Contact> selectAll(Integer pageNum, String cname) {
        PageHelper.startPage(pageNum,3);
        List<Contact> contacts=new ArrayList<>();
        if (cname!=null){
            ContactExample example=new ContactExample();
            ContactExample.Criteria criteria=example.createCriteria();
            criteria.andCnameLike("%"+cname+"%");
            contacts=contactMapper.selectByExample(example);
        }else{
            contacts=contactMapper.selectByExample(null);
        }
        PageInfo<Contact> pageInfo=new PageInfo<>(contacts);
        return pageInfo;
    }

    @Override
    public Integer saveContact(Contact contact) {
        return contactMapper.insertSelective(contact);
    }

    @Override
    public Contact selectById(Integer id) {
        return contactMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateContact(Contact contact) {
        return contactMapper.updateByPrimaryKeySelective(contact);
    }
}
